<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          :disabled="true"-->
      <!--          v-hasPermi="['money:monMstr:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['money:monMstr:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['money:monMstr:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['marketing:som:import']"
        >
          批量导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['money:monMstr:export']"
        >
          导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :height="tableH"
      :data="monMstrList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="客户代码"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="itmCustwldm"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="135"
        align="center"
        prop="monWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="350"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column label="材料单价(元)" width="100" align="center">
        <template #default="scope">
          <el-input-number
            v-model="scope.row.monUnitPrice"
            :controls="false"
            style="width: 78px"
            size="mini"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="$hasPermi(['money:monMstr:price'])"
        label="产品售价(元)"
        width="100"
        align="center"
      >
        <template #default="scope">
          <el-input-number
            v-model="scope.row.monSellingPrice"
            :controls="false"
            style="width: 78px"
            size="mini"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="$hasPermi(['money:monMstr:price'])"
        label="加工费(元)"
        width="90"
        align="center"
      >
        <template #default="scope">
          <el-input-number
            v-model="scope.row.monProcessingFees"
            :controls="false"
            style="width: 78px"
            size="mini"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="$hasPermi(['money:monMstr:price'])"
        label="加工工时(秒)"
        width="100"
        align="center"
      >
        <template #default="scope">
          <el-input-number
            v-model="scope.row.monJggs"
            :controls="false"
            style="width: 78px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="monJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="monJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.monJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="monGgry"
      />
      <el-table-column
        label="更改日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="monGgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.monGgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="100"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleInsertOrUpdateMonMstr(scope.row)"
            v-hasPermi="['money:monMstr:edit']"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="物料代码" prop="monWldm">
            <el-input
              v-model="queryParams.monWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户代码" prop="itmCustwldm">
            <el-input
              v-model="queryParams.itmCustwldm"
              placeholder="请输入客户代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="itmPmgg">
            <el-input
              style="width: 480px"
              type="textarea"
              v-model="queryParams.itmPmgg"
              placeholder="请输入综合查询"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 批量导入价格 -->
    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      :close-on-click-modal="false"
      width="400px"
      draggable
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">取 消</el-button>
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMonMstr,
  getMonMstr,
  delMonMstr,
  addMonMstr,
  updateMonMstr,
  exportMonMstr,
  insertOrUpdateMonMstr,
  exportMonExample,
} from "@/api/money/monMstr";
import { getToken } from "@/utils/auth";
import { exportMtpExample } from "@/api/money/mtpMstr";

export default {
  name: "MonMstr",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 300,
      // 选中数组
      monWldms: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 物料资产表格数据
      monMstrList: [],
      // 单位字典
      unitFlagOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        monWldm: null,
        itmCustwldm: null,
        itmPmgg: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/money/monMstr/importMon",
      },
    };
  },
  created() {
    //单位
    this.getAllDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询物料资产列表 */
    getList() {
      this.loading = true;
      // this.queryParams.pageNum = 1
      listMonMstr(this.queryParams).then((response) => {
        this.monMstrList = [];
        this.monMstrList = this.monMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    //load
    /*    load() {
      if (this.queryParams.pageNum < Math.ceil(this.total / this.queryParams.pageSize)) {
        this.queryParams.pageNum++
        listMonMstr(this.queryParams).then(response => {
          this.monMstrList = this.monMstrList.concat(response.rows)
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0
    },*/
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        monWldm: null,
        monUnitPrice: null,
        monSellingPrice: null,
        monProcessingFees: null,
        monJggs: null,
        monJlry: null,
        monJlrq: null,
        monGgry: null,
        monGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.monMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.monWldms = selection.map((item) => item.monWldm);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料资产";
    },
    /** 新增或修改按钮操作 */
    handleInsertOrUpdateMonMstr(row) {
      insertOrUpdateMonMstr(row).then((response) => {
        this.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const monWldm = row.monWldm || this.monWldms;
      if (row != null) {
        updateMonMstr(row).then((response) => {
          this.msgSuccess("修改成功");
          this.getList();
        });
      } else {
        getMonMstr(monWldm).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改物料资产";
        });
      }
    },
    handleSearch() {
      this.reset();
      this.title = "查询物料资产";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.monWldm != null) {
            updateMonMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMonMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const monWldms = row.monWldm || this.monWldms;
      this.$confirm(
        '是否确认删除物料资产编号为"' + monWldms + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMonMstr(monWldms);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有物料资产数据项?", "确认导出", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportMonMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    /** 分页 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    handleImport() {
      this.upload.title = "物料资产批量更新单价";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      console.log(response.msg);
      if (response.data) {
        this.download(response.data.msg);
      }
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 下载模板操作 */
    importTemplate() {
      exportMonExample().then((response) => {
        this.download(response.msg);
      });
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  ::v-deep .el-input {
    line-height: 23px;
    .el-input__inner {
      line-height: 23px;
      height: 23px;
      padding: 0;
    }
  }
}
</style>
